package JDBC;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Login {
    JTextField jt_name;
    JPasswordField jp_pass;
    ResultSet rs;
    public Login(){
        final JFrame jf=new JFrame();
        jf.setTitle("用户登陆界面");
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(350, 200);
        jf.setLocationRelativeTo(null);
        Container con=jf.getContentPane();
        con.setLayout(new GridLayout(3,1));
        JPanel pan1=new JPanel();
        JLabel name=new JLabel("用户名");
        jt_name=new JTextField(20);
        pan1.add(name);
        pan1.add(jt_name);
        con.add(pan1);
        JPanel pan2=new JPanel();
        JLabel pass=new JLabel("密码");
        jp_pass=new JPasswordField(20);
        pan2.add(pass);
        pan2.add(jp_pass);
        con.add(pan2);
        JPanel pan3=new JPanel();
        JButton jb_login=new JButton("登陆");
        jb_login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name=jt_name.getText();
                String no=new String(jp_pass.getPassword());
                String sql="SELECT * FROM user WHERE name='"+name+"' AND no='"+no+"';";
                rs= DbOp.getQuery(sql);
                try {
                    if (rs.next()){
                        JOptionPane.showMessageDialog(null, "登陆成功");
                        //new ShowMain();
                        jf.dispose();;
                    }
                }catch (HeadlessException e1){
                    e1.printStackTrace();;
                }catch (SQLException e1){
                    e1.printStackTrace();
                }
            }
        });
        JButton jb_exit=new JButton("退出");
        jb_exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        JButton jb_1=new JButton("修改密码");
        jb_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name=jt_name.getText();
                String no=new String(jp_pass.getPassword());
                String sql="UPDATE user SET no='"+no+"' WHERE username='"+name+"';";
                String  sqlQuery="SELECT * FROM userinfo WHERE username='"+name+"';";
                if (DbOp.getUpdate(sql)){
                    rs=DbOp.getQuery(sqlQuery);
                    try {
                        if (rs.next())
                            JOptionPane.showMessageDialog(null, "密码修改成功！\n用户名为：" + rs.getString(1) + "密码为："+rs.getString(2));
                        else {
                            JOptionPane.showMessageDialog(null, "注册失败！");
                        }
                    } catch (SQLException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });
        JButton jb_2=new JButton("注册");
        jb_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name=jt_name.getText();
                String no=new String(jp_pass.getPassword());
                String sql="INSERT INTO user(name,no)VALUES('"+name+"','"+no+"');";
                String  sqlQuery="SELECT * FROM user WHERE name'"+name+"';";
                if (DbOp.getUpdate(sql)){
                    JOptionPane.showMessageDialog(null, name+"用户注册成功!");
                    rs=DbOp.getQuery(sqlQuery);
                    try {
                        if (rs.next())
                            JOptionPane.showMessageDialog(null, "用户名为：" + rs.getString(1) + "密码为："+rs.getString(2));
                        else {
                            JOptionPane.showMessageDialog(null, "注册失败！");
                        }
                    }catch (SQLException e1) {
                        e1.printStackTrace();;
                    }
                }else
                    JOptionPane.showMessageDialog(null, "此用户已经存在，无法重新注册！");
            }
        });
        JButton jb_3=new JButton("删除");
        jb_3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String userName = jt_name.getText();
                String sql = "SELECT * FROM user WHERE name='" + name + "';";
                ResultSet rs = DbOp.getQuery(sql);
                try {
                    if (rs.next()) {
                        String sql1 = "delete from user where name='" + name  + "';";
                        boolean rs1 = DbOp.getUpdate(sql1);
                        if (rs1) {
                            JOptionPane.showMessageDialog(null, "删除成功");
                        } else {
                            JOptionPane.showMessageDialog(null, "删除失败");
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "你要删除的用户不存在！");
                    }
                } catch (SQLException e1) {
                    throw new RuntimeException(e1);
                }
            }
        });
        pan3.add(jb_login);
        pan3.add(jb_exit);
        pan3.add(jb_1);
        pan3.add(jb_2);
        pan3.add(jb_3);
        con.add(pan3);
    }

    public static void main(String[] args) {
        new Login();
    }

}
